package com.tnsw.facebookautoliker.helper;

import com.facebook.HttpMethod;
import com.facebook.LoggingBehavior;
import com.facebook.Request;
import com.facebook.RequestAsyncTask;
import com.facebook.Response;
import com.facebook.Session;
import com.facebook.Settings;

import android.app.Activity;
import android.content.Context;
import android.os.AsyncTask;
import android.util.Log;

public class TestFBAction1 extends FacebookCommandObject {

	private String id;
	private String tag = "TestFBAction1";
	
	public TestFBAction1(Context context, String id) {
		super(context);
		this.id = id;
	}
	
	@Override
	public void execute() {
		super.execute();
		
		Log.d(tag, "id=" + this.id);
		this.refreshFriendList(this.id);
		
	}
	
	public void refreshFriendList(final String id) {
		Settings.addLoggingBehavior(LoggingBehavior.REQUESTS);
		Session session = Session.openActiveSessionFromCache(this.context);
		
		final TestFBAction1 self = this;
		
		if (session != null) {
			String path = id + "/profile";
			
			final Request request = new Request(session, path, null, HttpMethod.GET, new Request.Callback() {
				
				@Override
				public void onCompleted(Response response) {
					Log.d(tag + "", "response_callback=" + response.toString());
					
					FacebookBGManager fbm = FacebookBGManager.getInstance(context);
					fbm.updateCommandStatus(self, 200, null);
					Log.d(tag, "concurrent=" + fbm.getConcurrent());
					
					if (response.getError() != null) {
						Log.e(tag, response.getError().getErrorCode() + ", " + response.getError().getErrorMessage());
					}
				}
			});
			
			//RequestAsyncTask task = new RequestAsyncTask(request);			
			//task.execute();
			//this.httpFirst.executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR, "0");
			
			//Log.d(tag, "executeAndWait()..");
			//Response response = request.executeAndWait();
			
			Activity activity = (Activity) this.context;
			activity.runOnUiThread(new Runnable() {
				
				@Override
				public void run() {
					RequestAsyncTask task = new RequestAsyncTask(request);			
					task.execute();
					
				}
			});

			//Log.d(tag, "response=" + response.toString());
			
		}

	}	

}
